package com.lak.nagivation.cal;

import com.lak.nagivation.Tool.JsonUtil;
import com.lak.nagivation.bean.Road;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CoreCal extends Exception {


    static String filepath="com\\lak\\nagivation\\data\\";
    public static void pretreatment(String[] roads){
        for (int i=0;i<roads.length;i++) {
            String road=roads[i];
            String url;
            if (road=="") {
                url = "com/lak/nagivation/data/" + "无名路" + ".json";
            }
            else {
                url = "com/lak/nagivation/data/" + road + ".json";
            }
            File file=new File(url);
            JsonUtil jsonU=new JsonUtil();
            String jsonStr = jsonU.readJsonFile(file);
            JSONObject json = JSON.parseObject(jsonStr);
            String name = json.getString("roadname");
            System.out.println(name);
        }
    }
    /**
    * @description 输入路名数组，在库里寻找对应的道路文件，返回道路类数组
    * @params [roads]道路名数组
    * @return  cal.Road[]道路类数组
    * @author  lj
    * @date  2020/7/27 10:57
    *etc：测试阶段，将所有
    */
    public static Road[] pretreatment_Road(String[] roads,String[] step_polyline){
        Road[] R=new Road[roads.length];
        for (int i=0;i<roads.length;i++) {
            String road=roads[i];
            String url;
            if (road=="") {
                url = filepath+"无名路.json";
            }
            else {
                url = filepath + road + ".json";
            }


            File file=new File(url);
            if (!file.exists()) {
                R[i] = new Road(road);
                R[i].polyline=step_polyline[i];
            }
            else {
                JsonUtil jsonU = new JsonUtil();
                String jsonStr = jsonU.readJsonFile(file);
                JSONObject json = JSON.parseObject(jsonStr);
                R[i] = new Road(json);
                R[i].polyline=step_polyline[i];
            }
        }
        return R;
    }
    /**
    * @description 处理导航信息
    * @params [instruction, stepPolyline]
    * @return  java.lang.String[]
    * @author  lj
    * @date  2020/8/6 10:13
    *
    */
    public static String[] pretreatment_Instruction(String[] instruction,String[] stepPolyline){
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<String> TZD= new ArrayList<String>(){
            {this.add("收费站");
            add("涵洞");
            add("匝道");}
        };
        String ss1 = "到达[\u4e00-\u9fa5]+";
        String ss2 = "进入[\u4e00-\u9fa5]+";
        Pattern pattern1 = Pattern.compile(ss1);
        Pattern pattern2 = Pattern.compile(ss2);
        for (int i = 0; i < instruction.length; i++) {
            String s =instruction[i];
            Matcher matcher1 = pattern1.matcher(s);
            Matcher matcher2 = pattern2.matcher(s);
            if (matcher1.find()){
                String re=matcher1.group().replace("到达","");
                if (TZD.contains(re)){
                    String polyline=stepPolyline[i];
                    String[] lnglat=polyline.split(",");
                    re+=","+lnglat[lnglat.length-2]+","+lnglat[lnglat.length-1];
                    list.add(re);
                }
            }
            if (matcher2.find()){
                String re=matcher2.group().replace("进入","");
                if (TZD.contains(re)){
                    String polyline=stepPolyline[i];
                    String[] lnglat=polyline.split(",");
                    re+=","+lnglat[lnglat.length-2]+","+lnglat[lnglat.length-1];
                    list.add(re);
                }
            }
        }
        int size = list.size();
        return (String[]) list.toArray(new String[size]);
    }


    //
    public static String[] pretreatment_Polyline(String[] Polyline){
        String[] sss=new String[Polyline.length/2];
        for (int i=0;i<Polyline.length/2;i++){
            sss[i]=Polyline[i*2]+","+Polyline[i*2+1];
        }
        String[] sss_de=de_duplication(sss);

        return sss_de;
    }
    /**
    * @description 数组去重
    * @params [arrStr]
    * @return  java.lang.String[]
    * @author  lj
    * @date  2020/7/27 11:33
    *
    */
    public static String[] de_duplication(String[] arrStr) {
        List<String> list = new ArrayList<String>();
        for (int i=0; i<arrStr.length; i++) {
            if(!list.contains(arrStr[i])) {
                list.add(arrStr[i]);
            }
        }

        //返回一个包含所有对象的指定类型的数组
        String[] newArrStr =  list.toArray(new String[1]);
        System.out.println(Arrays.toString(newArrStr));
        return newArrStr;
    }


    //处理check1 数据，返回“type，lag，lat”
    public  static String[] de_check1(String SSS,String[] stepPolyline){
        String[] cc=SSS.split("");
        String[] re=new String[cc.length];
        for (int i=0;i<cc.length;i++){

            String step=stepPolyline[i];
            String[] points=step.split(",");
            re[i]=cc[i]+","+points[points.length-2]+","+points[points.length-1];
        }
        return re;
    }

}
